package com.android.djlmapp.server;

import android.content.Context;
import android.util.Log;

import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.model.home.RefTokenInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * 闪屏页和服务里面刷新token工具类
 */

public class RefreshTokenTools {
    private int code;
    private String mmsg;
    private String token;
    private String version;
    private String network;
    private String user_name;
    private String user_password;
    private String push_id;
    private String phone_os;
    private String phone_type;
    private String phone_brand;
    private String phone_imei;
    private String phone_gps;
    private String phone_num;
    private Context mContext;

    private UserInfoUtils userInfoUtilsInstance;
    public RefreshTokenTools(Context context) {
        userInfoUtilsInstance = UserInfoUtils.getUserInfoUtilsInstance();
        this.mContext = context;
    }

    private StringCallback mStringCallback = new StringCallback() {
        @Override
        public void onSuccess(Response<String> response) {
            try {
                String body = response.body();
                Gson gson = new Gson();
                RefTokenInfo loginInfo = gson.fromJson(body, RefTokenInfo.class);
                code = loginInfo.getCode();
                mmsg = loginInfo.getMsg();
                if (code < 1000) {
                    token = loginInfo.getToken();

                    mRefreshTokenListener.refreshTokenSuc(token);
                } else {
                    Log.e("项目异常", "------mmsg------" + mmsg);
                    mRefreshTokenListener.refreshTokenFail();
                }
            } catch (Exception e) {
                mRefreshTokenListener.netError();
                ToastUtils.showShortToast(mContext, "数据解析异常");
            }
        }

        @Override
        public void onError(Response<String> response) {
            super.onError(response);
            Log.e("项目异常", "------过期token网络错误------" + response.body());

            mRefreshTokenListener.netError();
        }
    };

    public void refreshServ() {
        token = userInfoUtilsInstance.getToken();
        version = userInfoUtilsInstance.getVersion();
        network = userInfoUtilsInstance.getNetWork();
        user_name = userInfoUtilsInstance.getUserName();
        user_password = userInfoUtilsInstance.getPwd();
        push_id = userInfoUtilsInstance.getPushId();
        phone_os = userInfoUtilsInstance.getPhoneOs();
        phone_type = userInfoUtilsInstance.getPhoneType();
        phone_brand = userInfoUtilsInstance.getPhoneBrand();
        phone_imei = userInfoUtilsInstance.getPhoneImei();
        phone_gps = userInfoUtilsInstance.getPhoneGps();
        phone_num = userInfoUtilsInstance.getPhoneNum();


        MyApplication.getInstance().getHttpTools().login(network, phone_num, push_id,
                phone_brand, phone_gps, phone_imei, phone_os, phone_type,  "2",
                user_name, user_password,  mStringCallback);

    }

    private RefreshTokenListener mRefreshTokenListener;

    public interface RefreshTokenListener {
        void refreshTokenSuc(String token);

        void refreshTokenFail();

        void netError();
    }

    public void setRefreshTokenListener(RefreshTokenListener listener) {
        this.mRefreshTokenListener = listener;
    }

}
